package com.bw.controller;

import com.alibaba.excel.EasyExcel;
import com.bw.config.UploadMinioUtils;
import com.bw.domain.SysDictType;
import com.bw.domain.SysPost;
import com.bw.service.SysDictTypeService;
import com.bw.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/dictType")
public class SysDictTypeController {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    /**
     * 字典列表
     * @param sysDictType
     * @return
     */
    @RequestMapping("/dictList")
    public Result dictList(@RequestBody SysDictType sysDictType){
        List<SysDictType> list=sysDictTypeService.list(sysDictType);
        return Result.success(list);
    }

    /**
     * 字典添加
     * @param sysDictType
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody SysDictType sysDictType){
        sysDictType.setCreateTime(new Date());
        boolean b = sysDictTypeService.save(sysDictType);
        return b?Result.success("添加成功"):Result.fail("添加失败");
    }

    /**
     * 字典修改
     * @param sysDictType
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody SysDictType sysDictType){
        sysDictType.setCreateTime(new Date());
        boolean b = sysDictTypeService.update(sysDictType);
        return b?Result.success("修改成功"):Result.fail("修改失败");
    }

    /**
     * 字典删除 物理删除
     * @param dictId
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer dictId){
        boolean b = sysDictTypeService.removeById(dictId);
        return b?Result.success("删除成功"):Result.fail("删除失败");
    }

    /**
     * 字典表批删
     * @param ids
     * @return
     */
    @RequestMapping("/deletes")
    public Result deletes(String ids){
        List<String> asList = Arrays.asList(ids.split(","));
        sysDictTypeService.deletes(asList);
        return Result.success();
    }

    @RequestMapping("/getDictType")
    public Result getDictType(String dictType){
        sysDictTypeService.getDictTypeList(dictType);
        return Result.success();
    }

    /**
     * 字典导出excel
     * @param response
     * @throws IOException
     */
    @RequestMapping("/export")
    public void exportPostExcel(HttpServletResponse response) throws IOException {

        String fileName = URLEncoder.encode("字典信息", "UTF-8").replaceAll("\\+", "%20");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
        List<SysDictType> list=sysDictTypeService.list2();

        EasyExcel.write(response.getOutputStream(),SysDictType.class).sheet("字典信息").doWrite(list);


    }
    @RequestMapping("/addPic")
    public String addPic(MultipartFile file) {
        String s = UploadMinioUtils.uploadPic(file);
        return s;
    }

    @RequestMapping("/getxlk")
    public Result getxlk(){
        List<SysDictType> list =sysDictTypeService.getlist2();
        return Result.success(list);
    }


}
